import unittest


class TestListMethod(unittest.TestCase):

    """
    功能: 不同类型的list也可以使用+运算符进行连接
    """
    def test_list_append_all(self):
        list1 = [1, 2, 3, 4]
        list2 = ['s1', 's2', 's3', 's4']
        print(list1 + list2)

    def test_list_append(self):
        list1 = [1, 2, 3, 4, 5]
        list1.append(5)
        print("5出现的次数", list1.count(5))
        print(list1)

    """
    功能: list类型也支持切片操作;
    说明:
        1. 切片思路与字符串相同;
    """
    def test_range(self):
        list1 = [1, 2, 3, 4]
        print(list1[1:2])

    def test_get_value(self):
        list1 = [1, 2, 3, 4]
        # 可以像数组一样取值
        print(list1[2])

    """
    功能: list类型支持的运算符.
    说明:
        1. +表示连接;
        2. *表示复制;
        3. len表示计算长度;
        4. in表示检测是否包含;
    """
    def test_tuple_operation(self):
        list1 = [1, 2, 3, 4]
        print(list1 + list1)
        print(list1 * 2)
        print(len(list1))
        print(1 in list1)
        print(11 in list1)

    def test_foreach(self):
        list1 = [1, 2, 3, 4]
        for x in list1:
            print(x * 8)

    def test_list_to_dict(self):
        list1 = [{"key": "key1", "value": "value1"}, {"key": "key2", "value": "value2"}]
        dict = {}
        for item in list1:
            dict[item["key"]] = item["value"]
        print(dict)
        print(dict["key1"])
        print(dict.get("key1111"))

if __name__ == '__main__':
    unittest.main()
